package cn.tedu.jsd2210.csmall.product.controller;

import cn.tedu.jsd2210.csmall.product.pojo.dto.AlbumUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.AttributeTemplateAddNewDto;
import cn.tedu.jsd2210.csmall.product.pojo.dto.AttributeTemplateUpdateDto;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AttributeTemplateListVO;
import cn.tedu.jsd2210.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.jsd2210.csmall.product.service.IAttributeTemplateService;
import cn.tedu.jsd2210.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/attribute-template")
@Api(tags="02.属性模板管理模块")
@Validated
public class AttributeTemplateController {
    @Autowired
    private IAttributeTemplateService attributeTemplateService;
//http://localhost:9080/album/add-new?name=009
    @ApiOperation("添加属性模板")
    @PostMapping("/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeTemplateAddNewDto attributeTemplateAddNewDto){
            log.debug("开始处理添加属性模板的请求,参数:{}",attributeTemplateAddNewDto);
            attributeTemplateService.addNew(attributeTemplateAddNewDto);
            return JsonResult.ok();
    }

//    http://localhost:9080/album/delete?id=37
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除属性模板")
    @ApiOperationSupport(order = 200)
//    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "属性模板ID",required = true,dataType = "long",example = "100")
//        ,@ApiImplicitParam(name="username",value = "测试多参数用户名",required = true,dataType = "string")
//    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id){
        log.debug("开始处理根据id删除属性模板的请求,参数:{}",id);
        attributeTemplateService.delete(id);
        return JsonResult.ok();
    }
    //不用再try catch,只要出现异常,自动执行全局异常处理器类中方法
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性模板")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value = "属性模板ID",required = true,dataType = "long",example = "100")
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1L,message = "请提交有效的ID值!") Long id,
                                           @Valid AttributeTemplateUpdateDto attributeTemplateUpdateDto){
        log.debug("开始处理根据id{}修改属性模板的请求,新数据:{}",id,attributeTemplateUpdateDto);
        attributeTemplateService.updateInfoById(attributeTemplateUpdateDto,id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AttributeTemplateListVO>> list(){
        log.debug("开始处理根据查询属性模板列表的请求.无参数");
        List<AttributeTemplateListVO> list = attributeTemplateService.list();
        return JsonResult.ok(list);
//        JsonResult jsonResult = JsonResult.ok();
//        jsonResult.setData(list);
//        return jsonResult;
    }
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查找属性模板")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeTemplateStandardVO> getStandardById(
            @PathVariable @Range
                    (min = 1L,message = "请提交有效的ID值!")
                    Long id){
        log.debug("开始处理根据ID:{}查询属性模板列表的请求",id);
        AttributeTemplateStandardVO queryResult = attributeTemplateService.getStandardById(id);
        return JsonResult.ok(queryResult);
//        JsonResult jsonResult = JsonResult.ok();
//        jsonResult.setData(list);
//        return jsonResult;
    }

}
